package Interface;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import xmlItunes.Bibliotheque;
import Interface.Lecteur.PanelLecteurMp3;
import bislider.MyBiSlider;

import com.visutools.nav.bislider.BiSlider;
import com.visutools.nav.bislider.BiSliderAdapter;
import com.visutools.nav.bislider.BiSliderEvent;

public class panelOutils extends JPanel implements KeyListener{

	private static final long serialVersionUID = 1L;
	    final int MaxValueInfos = 15;
	    final Vector<Integer> SliderInfos = new Vector<Integer>();
		private MyBiSlider sliderAnnees = new MyBiSlider(SliderInfos);
		private HashMap<String, JCheckBox > listeCheckBoxGenres = new HashMap<String, JCheckBox >();  //  @jve:decl-index=0:
	    private FrameDemo saFenetrePrincipale ;
		private JPanel jPanelgenres = new JPanel();  //  @jve:decl-index=0:visual-constraint="12,153"
		private PanelLecteurMp3 panelLecteurmp3 = new PanelLecteurMp3();
		private JButton jbuttonSelectAll = new JButton("Tous");
		private JButton jbuttonDeSelectAll = new JButton("Aucun");
		private JCheckBox jcheckBoxAfficherLegende = new JCheckBox("Afficher les Legendes / Titres");
		private boolean afficherLegende = true;
		private panelJulien panelJulien = null ; 
		
		GridBagConstraints gridcontraitreTotal = new GridBagConstraints();
		
		/**
	 * This is the default constructor
	 */
	public panelOutils(FrameDemo fenetre) {
		super();
		this.saFenetrePrincipale = fenetre;
		initialize();
	}
	

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() 
	{
		this.panelJulien = new panelJulien( this.saFenetrePrincipale );
		
		 initialiserSliderAnnee();
		 jcheckBoxAfficherLegende.setSelected(false);
		 jcheckBoxAfficherLegende.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						if (jcheckBoxAfficherLegende.isSelected()) {
							afficherLegende = true;
							saFenetrePrincipale.getTabbedpanelDroite().getPanelUnivers().sonUnivers.mettreLegendepourTOUTEplanete();
							saFenetrePrincipale.getTabbedpanelDroite().getPanelCarre().afficherLegendes(true);
						}
						else
						{
							afficherLegende= false;
							saFenetrePrincipale.getTabbedpanelDroite().getPanelUnivers().sonUnivers.enleverLegendepourTOUTEplanete();
							saFenetrePrincipale.getTabbedpanelDroite().getPanelCarre().afficherLegendes(false);
						}
					

				}
			}); 
		 
		 this.setLayout(new GridBagLayout());
		
		gridcontraitreTotal.gridx = 0;
		gridcontraitreTotal.gridy = 0;
		gridcontraitreTotal.fill = GridBagConstraints.HORIZONTAL;
		this.add(sliderAnnees,gridcontraitreTotal) ;
		
		 jPanelgenres.setLayout(new GridBagLayout());		
		 creerCheckBoxGenres();
		 ajouterListenerSurSlider();
		 creerBoutonsSelectAll();
		
		 gridcontraitreTotal.gridx = 0;
		 gridcontraitreTotal.gridy = 1;
		 this.add(new JPanel(), gridcontraitreTotal); //blanc
		 gridcontraitreTotal.gridx = 0;
		 gridcontraitreTotal.gridy = 2;
		 jPanelgenres.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY), "Genres",TitledBorder.CENTER,TitledBorder.CENTER));
		 this.add(jPanelgenres,gridcontraitreTotal);
		 
		 gridcontraitreTotal.gridx = 0;
		 gridcontraitreTotal.gridy = 3;
		 this.add(new JPanel(), gridcontraitreTotal); //blanc
		 
		 gridcontraitreTotal.gridx = 0;
		 gridcontraitreTotal.gridy = 4;

		 this.add(jcheckBoxAfficherLegende,gridcontraitreTotal);
	
		 gridcontraitreTotal.gridx = 0;
		 gridcontraitreTotal.gridy = 5;
		 this.add(new JPanel(), gridcontraitreTotal); //blanc
		 
		 //afficher panelJulien si on est dessus :
		 saFenetrePrincipale.getTabbedpanelDroite().addChangeListener(new ChangeListener() {		
			@Override
			public void stateChanged(ChangeEvent e) {
				 if (saFenetrePrincipale.getTabbedpanelDroite().getSelectedIndex() == 1) {
					 activerPanelJulien(true);
				 }
				 else {
					 activerPanelJulien(false);
				 }
			}
		});
		 	 
		 gridcontraitreTotal.gridx = 0;
		 gridcontraitreTotal.gridy = 7;
		 this.add(panelLecteurmp3,gridcontraitreTotal);
		 
	}
	
	private void activerPanelJulien(boolean etat) {
		if (etat) {
			 gridcontraitreTotal.gridx = 0;
			 gridcontraitreTotal.gridy = 6;
			 panelJulien.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY), "Preferences",TitledBorder.CENTER,TitledBorder.CENTER));
			 
			 if( this.getComponent(0) == panelJulien )
			 {
				 this.getComponent(0).setVisible(true);
				 
			 }
			 else
			 {
				 this.add(panelJulien,gridcontraitreTotal,0);
			 }
			 
			 
		}
		else  
		{
			this.getComponent(0).setVisible(false);
		}
		
			
	}
	
	private void initialiserSliderAnnee() {
		 Bibliotheque bib = saFenetrePrincipale.getSaBib(); 
		 int borneInf = bib.getAnneeBorneInf();
		 sliderAnnees.setMinimumValue(bib.getAnneeBorneInf());
		 sliderAnnees.setMaximumValue(bib.getAnneeBorneSup());
		 sliderAnnees.setColoredValues(bib.getAnneeBorneInf(),bib.getAnneeBorneSup());

		 int ecartAnnee = bib.getAnneeBorneSup() - bib.getAnneeBorneInf();
		 //on prend lecart ex : 80 on enleve le dernier chiffre ca nous donne la taille dun sgment : 8
		 String ecartAnneeString = "" + ecartAnnee;
		 ecartAnneeString = ecartAnneeString.substring(0, ecartAnneeString.length()-1);
		// sliderAnnees.setSegmentSize(Integer.parseInt(ecartAnneeString));
		
		 for (int i=0 ; i < ecartAnnee ; i++ ) {
			 SliderInfos.add(i, bib.getNbTitreDeLAnnee(i+borneInf)); // colonne 0 : contient 3 unit�s (titre, album, ...)
		 }
		 
		 sliderAnnees.setInterpolationMode(BiSlider.CENTRAL);
	}
	
	public void creerCheckBoxGenres() {
		Bibliotheque bib = saFenetrePrincipale.getSaBib(); 
		 //ajout des checkbox des genres :
		 	int x = 0,y = 1,cpt=0; //position sur le grid
		 	//creation select all / deselect all :
		 	
		 	GridBagConstraints gridcontrainteTout = new GridBagConstraints();
		 	gridcontrainteTout.gridx = 0;
		 	gridcontrainteTout.gridy = 0;
		 	gridcontrainteTout.fill = GridBagConstraints.HORIZONTAL;
		 	//jbuttonSelectAll.setSize(100,100);
		 	jPanelgenres.add(jbuttonSelectAll,gridcontrainteTout);
		 	jbuttonSelectAll.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						GenreToutSelect();
				}
			});
		 	gridcontrainteTout.gridx = 1;
		 	gridcontrainteTout.gridy = 0;
		 	jPanelgenres.add(jbuttonDeSelectAll,gridcontrainteTout);
		 	jbuttonDeSelectAll.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						GenreToutDeSelect();
				}
			});
		 	
		 	
			for (String s : bib.getGenres().keySet()) {
				JCheckBox checkBoxCourante = new JCheckBox(s);
				
				CheckBoxGenreHandler gestionnaire = new CheckBoxGenreHandler();
				 checkBoxCourante.addItemListener( gestionnaire);
				listeCheckBoxGenres.put(s, checkBoxCourante);
				GridBagConstraints gridcontrainte = new GridBagConstraints();
				gridcontrainte.gridx = x;
				gridcontrainte.gridy = y;
				gridcontrainte.fill = GridBagConstraints.HORIZONTAL;
				jPanelgenres.add(checkBoxCourante,gridcontrainte);
				if (x == 0) x = 1; else x=0;
				if (cpt % 2 == 1) y++;
				cpt++;
			}
		
	}
	
	 public void creerBoutonsSelectAll() {}
	 
	 
	public FrameDemo getSaFenetrePrincipale() {
		return saFenetrePrincipale;
	}

	public void setSaFenetrePrincipale(FrameDemo saFenetrePrincipale) {
		this.saFenetrePrincipale = saFenetrePrincipale;
	}

	 private class CheckBoxGenreHandler implements ItemListener {
	
		public void itemStateChanged(ItemEvent e) {
			int sliderMin = (int) sliderAnnees.getColorTable()[0][0];
 			int sliderMax =  (int) sliderAnnees.getColorTable()[sliderAnnees.getColorTable().length -1][1];
        	saFenetrePrincipale.getTabbedpanelDroite().getPanelUnivers().sonUnivers.MAJUniversSelonGenresAnnees(saFenetrePrincipale.getSaBib(),listeCheckBoxGenres,sliderMin, sliderMax);
        	
        	saFenetrePrincipale.getTabbedpanelDroite().getPanelCarre().selectionGenreAnnee(listeCheckBoxGenres , sliderMin , sliderMax);
		}
	
	}
	 
	 public void cocherNPremieresCasesGenre(int n) {
			Bibliotheque bib = saFenetrePrincipale.getSaBib(); 
			if (n > bib.getGenres().size()) n = bib.getGenres().size(); //si n depasse le nbre de cases on met n au max 
			Iterator<String> it = bib.getGenres().keySet().iterator();
			for (int i = 0; i< n ; i++){
				String s = it.next();
				listeCheckBoxGenres.get(s).setSelected(true);
			}
			
			int sliderMin = (int) sliderAnnees.getColorTable()[0][0];
 			int sliderMax =  (int) sliderAnnees.getColorTable()[sliderAnnees.getColorTable().length -1][1];
        	saFenetrePrincipale.getTabbedpanelDroite().getPanelUnivers().sonUnivers.MAJUniversSelonGenresAnnees(saFenetrePrincipale.getSaBib(),listeCheckBoxGenres,sliderMin, sliderMax);
		}
	 
	 
	 private void ajouterListenerSurSlider(){
			
		 this.sliderAnnees.addBiSliderListener(new BiSliderAdapter(){
		        /** something changed that modified the color gradient between min and max */
		        @Override
		        public void newColors(BiSliderEvent BiSliderEvent_Arg) {
		        }
		        /**  min or max values changed  */  
		        @Override
		        public void newValues(BiSliderEvent BiSliderEvent_Arg) {
					//saFenetrePrincipale.getPanelGauche().getPanelUnivers().sonUnivers.ActiverPlaneteSelonAnnee(saFenetrePrincipale.getSaBib(),(int)BiSliderEvent_Arg.getMinimum(), (int)BiSliderEvent_Arg.getMaximum());
					int sliderMin = (int) sliderAnnees.getColorTable()[0][0];
		 			int sliderMax =  (int) sliderAnnees.getColorTable()[sliderAnnees.getColorTable().length -1][1];
		        	saFenetrePrincipale.getTabbedpanelDroite().getPanelUnivers().sonUnivers.MAJUniversSelonGenresAnnees(saFenetrePrincipale.getSaBib(),listeCheckBoxGenres,sliderMin, sliderMax);
		        	saFenetrePrincipale.getTabbedpanelDroite().getPanelCarre().selectionGenreAnnee(listeCheckBoxGenres , sliderMin , sliderMax);
		        }
		        /**  min selected value changed  */
		        @Override
		        public void newMinValue(BiSliderEvent BiSliderEvent_Arg) {
		        }
		        
		        @Override
		        public void newMaxValue(BiSliderEvent BiSliderEvent_Arg) {         
		        }
		        /**  selected segments changed  */
		        @Override
		        public void newSegments(BiSliderEvent BiSliderEvent_Arg) {
		        
		        }
		    });  
		 
	}


	public PanelLecteurMp3 getPanelLecteurmp3() {
		return panelLecteurmp3;
	}


	@Override
	public void keyPressed(KeyEvent e) {
		
	}


	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public void	GenreToutSelect(){
		for (JCheckBox j : listeCheckBoxGenres.values()) {
			j.setSelected(true);
		}
	}

	public void	GenreToutDeSelect(){
		for (JCheckBox j : listeCheckBoxGenres.values()) {
			j.setSelected(false);
		}
	}


	public boolean isAfficherLegende() {
		return afficherLegende;
	}


	public void setAfficherLegende(boolean afficherLegende) {
		this.afficherLegende = afficherLegende;
	}
	
} 
